package tests;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import sessions.LoginSystemRemote;
import sessions.friendshipSession.FriendshipSessionRemote;
import sessions.requestFriendshipSession.FriendshipRequestSessionRemote;
import entities.Friendship;

public class FriendshipTests {
	
	private static Context ctx;
	private int userId1,userId2,userId3;

	/**
	 * Riferimento alle varie interfacce remote
	 */
	private static FriendshipRequestSessionRemote friendshipRequestSessionRemote;
	private static LoginSystemRemote loginSystemRemote;
	private static FriendshipSessionRemote friendshipSessionRemote;
	
	public FriendshipTests() throws Exception{
		
		ctx = getInitialContext();
		loginSystemRemote=(LoginSystemRemote) ctx
				.lookup("Progetto/LoginSystem/remote");
		friendshipSessionRemote=(FriendshipSessionRemote) ctx
				.lookup("Progetto/FriendshipSession/remote");
		friendshipRequestSessionRemote=(FriendshipRequestSessionRemote) ctx
				.lookup("Progetto/FriendshipRequestSession/remote");
	
	}
	
	@Before
	public void setUp(){
		userId1 = loginSystemRemote.insertUser("matteo.neri@hotmail.it", "Matteo", "Neri", "MATTEO", "1980/01/01", "023457234", "3334256712");
		userId2 = loginSystemRemote.insertUser("andrea.bianchi@yahoo.it", "Andrea", "Bianchi", "ANDREA", "1988/02/03", "062345654", "3490861235");
		userId3 = loginSystemRemote.insertUser("luigi.verdi@yahoo.it", "Luigi", "Verdi", "LUIGI", "1983/05/03", "0832314567", "3271094529");
	}
	
	@After
	public void eraseAllData(){
		loginSystemRemote.eraseUser(userId1);
		loginSystemRemote.eraseUser(userId2);
		loginSystemRemote.eraseUser(userId3);
	}
	
	/**
	 * verifica che funzioni la rimozione di un'amicizia dal database
	 * @throws ParseException 
	 */
	@Test
	public void friendshipRemoved() throws ParseException{
		
		DateFormat df = new SimpleDateFormat ("yyyy/mm/dd");
		df.setLenient (false);
		Date d;

			d = df.parse ("2013/01/01");
		
		int idRequest=friendshipRequestSessionRemote.insertFriendshipRequest(userId1, userId2, d, true, false);
		friendshipRequestSessionRemote.acceptRequest(idRequest);
		
		friendshipSessionRemote.eraseFriendship(userId1, userId2);
		
		Assert.assertFalse(friendshipSessionRemote.checkFriendships(userId1, userId2));
		
	}
	
	/**
	 * verifica che funzioni il metodo getUserFriendships
	 * @throws ParseException 
	 */
	@Test
	public void userFriendships() throws ParseException{
		ArrayList<Friendship> friendship;
		DateFormat df = new SimpleDateFormat ("yyyy/mm/dd");
		df.setLenient (false);
		Date d;

			d = df.parse ("2013/01/01");
		
		int idRequest1=friendshipRequestSessionRemote.insertFriendshipRequest(userId1, userId2, d, true, false);
		int idFriendship1=friendshipRequestSessionRemote.acceptRequest(idRequest1);
		int idRequest2=friendshipRequestSessionRemote.insertFriendshipRequest(userId1, userId3, d, true, false);
		int idFriendship2=friendshipRequestSessionRemote.acceptRequest(idRequest2);

		friendship = friendshipSessionRemote.getUserFriendships(userId1);
				
		Assert.assertTrue(friendship.get(0).getId()==idFriendship1&&
				friendship.get(1).getId()==idFriendship2&&
				friendship.size()==2);
		
	}
	
	/**
	 * verifica che funzioni il metodo checkFriendships
	 * @throws ParseException 
	 */
	@Test
	public void checkFriendship() throws ParseException{
		
		DateFormat df = new SimpleDateFormat ("yyyy/mm/dd");
		df.setLenient (false);
		Date d;

			d = df.parse ("2013/01/01");
		
		int idRequest=friendshipRequestSessionRemote.insertFriendshipRequest(userId1, userId2, d, true, false);
		friendshipRequestSessionRemote.acceptRequest(idRequest);
		
		Assert.assertTrue(friendshipSessionRemote.checkFriendships(userId1, userId2));
		
	}
	
	/**
	 * Ottiene il Context utilizzato in seguito per ottenere i riferimenti ai RemoteSystems
	 * @return Context utilizzato per la lookUp
	 * @throws Exception Eccezione lanciata nel caso il metodo non riesca ad ottenere il Context
	 */


	static public Context getInitialContext() throws Exception{

		Hashtable<String,String> env = new Hashtable<String,String>();

		env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
		env.put(Context.PROVIDER_URL, "localhost:1099");

		return new InitialContext(env);

		}
	


}
